/**
 * This is a listener for buy
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Master
 * @version $Revision: 1.0 $
 */
public class BuyListener implements ActionListener {
	/**
	 * Field FIVE.
	 * (value is 5)
	 */
	private static final int FIVE = 5;

	/**
	 * Field TWENTY.
	 * (value is 20)
	 */
	private static final int TWENTY = 20;

	/**
	 * Method toString
	 * @return String */
	@Override
	public String toString() {
		return "BuyListener [planet=" + planet + ", good=" + good + ", mps=" + getMps()
				+ ", gameInfo=" + gameInfo + ", index=" + index + "]";
	}
	
	/**
	 * Field p.
	 */
	public Planet planet;
	
	/**
	 * Field good.
	 */
	public String good="";
	
	/**
	 * Field mps.
	 */
	private MarketPlaceScreen mps;
	
	/**
	 * Field gameInfo.
	 */
	public GameInfo gameInfo;
	
	/**
	 * Field index.
	 */
	public int index=0;
	
	/**
	 * Constructor for BuyListener.
	 * @param good String
	 * @param info GameInfo
	 * @param planet Planet
	 */
	public BuyListener(Planet planet, String good, GameInfo info) {
		this.planet=planet;
		this.good=good;
		setMps(new MarketPlaceScreen());
		gameInfo=info;
		for (int i=0;i < GoodInfo.GoodName.length;i++){
			if (GoodInfo.GoodName[i].equals(good)) index=i;
		}
	}
	
	/**
	 * Method actionPerformed.
	 * @param arg0 ActionEvent
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (!canMatchTechLevel()) {
			getMps().upDate("The tech level is too low");
		    return;
		}
		if (!canMatchBudget()){
			getMps().upDate("You don't have enough money");
			return;
		}
		if (!canMatchCargo()){
			getMps().upDate("You don't have enough cargo space");
			return;
		}
		if (GameInfo.getTraderSP() >= FIVE) {
			PlayerInfo.buy(index, GoodInfo.getMODELEDPRICE()[index] *
					(TWENTY - GameInfo.getTraderSP() / TWENTY));
		}else {
			PlayerInfo.buy(index, GoodInfo.getMODELEDPRICE()[index]);
		}
		updateLabels();
	}
	
	/**
	 * Method updateLabels.
	 */
	public void updateLabels(){
		MarketPlaceScreen.Money.setText("" + PlayerInfo.getMoney());
		MarketPlaceScreen.Space.setText("" + PlayerInfo.getCargoAvailable());
		for (int i=0;i < GoodInfo.GoodName.length;i++){
			MarketPlaceScreen.Stock[i].setText("" + PlayerInfo.getStock(i) + "");
		}
	}
	
	/**
	 * Method checkTechLevel.
	 * @return boolean */
	public boolean canMatchTechLevel(){
		return (planet.getSolarSystem().getTechLevel() >= GoodInfo.getMP()[index]);
	}
	
    /**
     * Method checkBudget.
     * @return boolean */
    public boolean canMatchBudget(){
    	return (GoodInfo.getMODELEDPRICE()[index] <= PlayerInfo.getMoney());
    }
    
    /**
     * Method checkCargo.
     * @return boolean */
    public boolean canMatchCargo(){
    	return (PlayerInfo.getCargoAvailable() > 0);
    }

	/**
	 * Method getMps.
	 * @return MarketPlaceScreen
	 */
	public MarketPlaceScreen getMps() {
		return mps;
	}

	/**
	 * Method setMps.
	 * @param mps MarketPlaceScreen
	 */
	public void setMps(MarketPlaceScreen mps) {
		this.mps = mps;
	}
}

